﻿using System;
using System.Web;

namespace Hongapo_Common
{
    public sealed class PRequest
    {
        private PRequest() { }

        private static bool InvalidRequest(bool byPost)
        {
            return (byPost && HttpContext.Current.Request.RequestType == "GET");
        }

        public static int GetInt32(string name)
        {
            return GetInt32(name, 0, false);
        }

        public static int GetInt32(string name, bool byPost)
        {
            return GetInt32(name, 0, byPost);
        }

        public static int GetInt32(string name, int defaultValue)
        {
            return GetInt32(name, defaultValue, false);
        }

        public static int GetInt32(string name, int defaultValue, bool byPost)
        {
            if (InvalidRequest(byPost))
                return defaultValue;

            if (string.IsNullOrEmpty(HttpContext.Current.Request[name]))
                return defaultValue;

            int result;

            if (!int.TryParse(HttpContext.Current.Request[name], out result))
                result = defaultValue;

            return result;
        }

        public static decimal GetDecimal(string name)
        {
            return GetDecimal(name, 0, false);
        }

        public static decimal GetDecimal(string name, bool byPost)
        {
            return GetDecimal(name, 0, byPost);
        }

        public static decimal GetDecimal(string name, decimal defaultValue)
        {
            return GetDecimal(name, defaultValue, false);
        }

        public static decimal GetDecimal(string name, decimal defaultValue, bool byPost)
        {
            if (InvalidRequest(byPost))
                return defaultValue;

            if (string.IsNullOrEmpty(HttpContext.Current.Request[name]))
                return defaultValue;

            decimal result;

            if (!decimal.TryParse(HttpContext.Current.Request[name], out result))
                result = defaultValue;

            return result;
        }

        public static string GetString(string name)
        {
            return GetString(name, string.Empty, false);
        }

        public static string GetString(string name, string defaultValue)
        {
            return GetString(name, defaultValue, false);
        }

        public static string GetString(string name, bool byPost)
        {
            return GetString(name, string.Empty, byPost);
        }

        public static string GetString(string name, string defaultValue, bool validateSafe)
        {
            return GetString(name, defaultValue, false, validateSafe);
        }

        public static string GetString(string name, string defaultValue, bool byPost, bool validateSafe)
        {
            if (InvalidRequest(byPost))
                return defaultValue;

            if (null == HttpContext.Current.Request[name])
                return defaultValue;

            string value = HttpContext.Current.Request[name].Trim();

            if (validateSafe && !PageValidate.IsSafeSqlString(value))
                return defaultValue;

            return value;
        }
    }
}
